import time
import os

from django.http import JsonResponse
from common.merged_backend import *

from mainapp.models import *
from common.error import *
from news import app

# text_file = '155832'
# press_release = merged_writebot(text_file)
# print(press_release)

from django.http import JsonResponse
from common.merged_backend import *

from common.error import *
from news import app
import math


# text_file = '155832'
# press_release = merged_writebot(text_file)
# print(press_release)


@app.task
def create_news(news_type, videos, title, news_id):
    print(news_type)
    create_news.update_state(state='PROGRESS', meta={'progress': 5})
    # video_file = videos[0].split('\\')[-1].split('.')[0]
    i = 1
    m = len(videos) * 3 + 1
    for video in videos:
        video_file = video.split('\\')[-1].split('.')[0]
        ret_data_process = merged_data_process(video_file, news_id)
        message_out(ret_data_process)
        p = math.ceil(i / m * 100)
        create_news.update_state(state='PROGRESS', meta={'progress': p})
        i += 1
        ret_video_test = merged_video_test(video_file, news_id)  # 这里需要接上message_out 才会正常.
        message_out(ret_video_test)
        p = math.ceil(i / m * 100)
        create_news.update_state(state='PROGRESS', meta={'progress': p})
        i += 1
        ret_video_visualise = merged_video_visualise(video_file, news_id)  # 这里需要接上message_out 才会正常.
        message_out(ret_video_visualise)
        p = math.ceil(i / m * 100)
        create_news.update_state(state='PROGRESS', meta={'progress': p})
        i += 1
    text_file = news_id
    press_release = merged_writebot(text_file)
    data = {
        'news_id': news_id,
        'title': title,
        'news_type': news_type,
        'press_release': press_release
    }
    info = {
        'successful': True,
        'data': data,
        'code': ERROR_CODE_OPERATION_SUCCESS,
        'message': '新闻制作成功'
    }
    return JsonResponse(info, json_dumps_params={'ensure_ascii': False})
